home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6148 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  68 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help With Pointers
  5. Date: 22 Feb 1996 12:09:10 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4giih6INNdr8@keats.ugrad.cs.ubc.ca>
  8. References: <4g67cj$6cv@hobbes.compusult.nf.ca>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4g67cj$6cv@hobbes.compusult.nf.ca>,
  12. Barry A. Ryan <bryan@public.compusult.nf.ca> wrote:
  13.  
  14. >I'am actually trying to read SGI binary data with a PC.  I've been told
  15. >that the SGI bytes ( not bits ) are rotated versus a PC.  Any help 
  16.  
  17. Try using Sun XDR, which is standard for external data representation. If you
  18. write the data through XDR on one machine, you should be able to read it on the
  19. other machine. The XDR standard is not super robust, but on the other hand it
  20. is efficient and fairly compact. For float numbers, it uses IEEE format. If
  21. both machines use IEEE, XDR has no problem.
  22.  
  23. #include <rpc/rpc.h>
  24.  
  25. /*
  26.  * write a float on standard output
  27.  */
  28.  
  29. int main()
  30.  
  31. {
  32.     XDR out;        /* XDR stream structure    */
  33.     double mynum = 3.0;
  34.  
  35.     /* set up XDR stream to encode via standard output    */
  36.  
  37.     xdrstdio_create(&out,stdout,XDR_ENCODE);
  38.  
  39.     /* encode ``mynum''    */
  40.  
  41.     xdr_double(&out,&mynum);
  42. }
  43.  
  44.  
  45. To decode from standard input, change the "stdout" to "stdin" and "XDR_ENCODE"
  46. to "XDR_DECODE". Everything else stays the same.
  47.  
  48. XDR has way to decode to and from memory buffers, and record streams (via
  49. TCP/IP stream sockets).
  50.  
  51. I use XDR extensively. In a recent project, a Windows programmer has built a
  52. client which communicates with my UNIX-based server. To compile the requestor
  53. protocol, he had to port XDR to the 16-bit environment, which went without a
  54. hitch. Though it's not part of the C standard, it is a mini standard in itself
  55. that is widely available on UNIX systems, is available in freely distributable
  56. form, and can be ported to 16-bit PC environments (which explains why PC-NFS
  57. works! XDR/RPC is the basis for all the Sun NFS/NIS software).
  58.  
  59. XDR does the byte order conversions for you. There is a definition language for
  60. specifying complex structures, which automatically compile into C headers with
  61. structure declarations and recursive encoding/decoding C functions that you can
  62. compile and link with your project. Thus you can use the XDR language to make a
  63. structure "foo", and the "rpcgen" compiler will write a C function xdr_foo()
  64. for encoding/decoding that structure using the atomic primitives, along with C
  65. declarations.
  66. -- 
  67.  
  68.